﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using System.Web.UI.HtmlControls;
using BLL.Common;
using Enum;

namespace App
{
    public partial class User_UserList : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                pageInit();
            }
        }

        protected void rptUserList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                UserProfileInfo u = e.Item.DataItem as UserProfileInfo;
                HtmlGenericControl userImage = e.Item.FindControl("userImage") as HtmlGenericControl;
                HtmlAnchor userLink = e.Item.FindControl("userLink") as HtmlAnchor;
                Label lbl_nickName = e.Item.FindControl("lbl_nickName") as Label;
                Label lbl_address = e.Item.FindControl("lbl_address") as Label;

                Label lbl_Province = e.Item.FindControl("lbl_Province") as Label;
                Label lbl_city = e.Item.FindControl("lbl_city") as Label;

                HtmlAnchor linkUserRegion = e.Item.FindControl("linkUserRegion") as HtmlAnchor;

                if (u != null && u.UserPassportId > 0 && userImage != null && userLink != null && lbl_nickName != null && lbl_address != null && lbl_Province != null && lbl_city != null && linkUserRegion != null)
                {
                    userImage.Attributes["src"] = UploadResource.GetPath(BizResourceType.UserResource, BizResourceAccess.Display) + u.UserImage;
                    userLink.HRef = Contexts.AppPath + "/User/UserInfo.aspx?" + Contexts.UserPassportId + "=" + Contexts.GetEncrypt(u.UserPassportId.ToString()) + "&" + Contexts.UserType + "=" + Contexts.GetEncrypt(Contexts.GetUserType().ToString());
                    userLink.Title = u.NickName;
                    if (string.IsNullOrEmpty(u.NickName))
                    {
                        lbl_nickName.Text = "不详";
                        lbl_nickName.ToolTip = u.NickName;
                    }
                    else
                    {
                        lbl_nickName.Text = u.NickName.Length > 6 ? u.NickName.Substring(0, 6) + "..." : u.NickName;
                        lbl_nickName.ToolTip = u.NickName;
                    }
                    if (string.IsNullOrEmpty(u.Adress))
                    {
                        lbl_address.Text = "不详";
                    }
                    else
                    {

                        lbl_address.Text = u.Adress.Length > 6 ? u.Adress.Substring(0, 6) + "..." : u.Adress;
                    }

                    //省市
                    List<AreaInfo> allArea = AppAuthHelper.GetAllArea();
                    AreaInfo curent = allArea.Find(delegate(AreaInfo o)
                    {
                        if (o.AreaCode == u.ProvinceId)
                            return true;
                        return false;
                    });
                    if (curent != null && curent.AreaCode > 0)
                    {
                        lbl_Province.Text = curent.AreaName;
                    }
                    curent = null;
                    curent = allArea.Find(delegate(AreaInfo o)
                    {
                        if (o.AreaCode == u.CityId)
                            return true;
                        return false;
                    });
                    if (curent != null && curent.AreaCode > 0)
                    {
                        lbl_city.Text = curent.AreaName;
                    }
                    linkUserRegion.HRef = userLink.HRef;
                    linkUserRegion.InnerText = "[进入空间]";
                }

            }
            if (e.Item.ItemType == ListItemType.Footer)
            {
                if (rptUserList.Items.Count <= 0)
                {
                    ((Label)e.Item.FindControl("lblNoRecord")).Visible = true;
                }
            }
        }
        protected void anpOrders_PageChanged(object sender, EventArgs e)
        {
            bindUsers();
        }
        private void pageInit()
        {
            bindUsers();
        }

        private void bindUsers()
        {
            int userType = Contexts.GetUserType();
            List<UserProfileInfo> userProfiles = null;
            switch (userType)
            {
                case (int)UserType.Empower:
                    this.lbl_UserType.Text = "自强会员";
                    userProfiles = BLL.BUserProfile.getInstance().GetALLUserProfileByUserType((int)UserType.Empower);
                    break;
                case (int)UserType.Angel:
                    this.lbl_UserType.Text = "天使会员";
                    userProfiles = BLL.BUserProfile.getInstance().GetALLUserProfileByUserType((int)UserType.Angel);
                    break;
                case (int)UserType.Admin:
                    this.lbl_UserType.Text = "管理员";
                    userProfiles = BLL.BUserProfile.getInstance().GetALLUserProfileByUserType((int)UserType.Admin);
                    break;
                case 0:
                    this.lbl_UserType.Text = "所有会员";
                    userProfiles = BLL.BUserProfile.getInstance().GetALLUserProfile();
                    userProfiles.RemoveAll(delegate(UserProfileInfo o)
                    {
                        if (o.UserType == (int)UserType.Admin)
                            return true;
                        else
                            return false;
                    });
                    break;

            }

            if (userProfiles != null)
            {
                this.anpOrders.RecordCount = userProfiles.Count;
                List<UserProfileInfo> temp = new List<UserProfileInfo>();
                temp.AddRange(userProfiles);
                int currentNum = 0;

                //分页数据处理
                foreach (UserProfileInfo info in temp)
                {
                    currentNum++;
                    if (currentNum < anpOrders.PageSize * (anpOrders.CurrentPageIndex - 1) + 1 || currentNum > anpOrders.PageSize * anpOrders.CurrentPageIndex)
                    {
                        userProfiles.Remove(info);
                    }
                }
                this.rptUserList.DataSource = userProfiles;
                this.rptUserList.DataBind();
            }
            //if (userProfiles != null)
            //{


            //    this.rptUserList.DataSource = userProfiles;
            //    this.rptUserList.DataBind();
            //}
        }
    }
}